extends HBoxContainer

export var is_more = true


onready var sprite_op = $SpriteSelectPlus
onready var star_op = $star
onready var prop_op = $ItemSelect

# 战斗宝可梦选项节点
var diy_data = ""

func _ready():
	sprite_op.set_value(1)
	prop_op.set_value(-1)



func set_data(data, is_more = true):
	$star.visible = is_more
	$SpinBox.visible = is_more
	
	if data:
		sprite_op.set_value(data["pokeId"])
		star_op.select(star_op.get_item_index(data["star"]))
		$SpinBox.value = data["level"]
		prop_op.set_value(data["attachItemId"])
		diy_data = data.get("spriteJson","")


# 获取数据
func get_sprite_data():
	var result = {
		"pokeId": sprite_op.get_value(),
		"star": star_op.get_selected_id(),
		"level": $SpinBox.value,
		"attachItemId": prop_op.get_value(),
	}
	if diy_data:
		var json = JSON.parse(diy_data)
		if json.result is Dictionary:
#		if json.error == OK:
			result["spriteJson"] = json.result
		else:
			MainTool.toast_info("自定义json格式错误")
			pass
	return result

# 点击删除
func _on_del_button_up():
	queue_free()

# 点击自定义，弹出输入框，供其输入，然后保存回去
func _on_Button_pressed():
	var ui = MainTool.getUILayer().add_ui("res://tool/common/PopUp/TextEditPopup.tscn", 0, false)
	ui.popup_centered()
	ui.set_data(diy_data,funcref(self, "_on_diy_data"))

func _on_diy_data(state,diy):
	if state == 0: return

	var json = JSON.parse(diy)
	if json.result is Dictionary:
		diy_data = diy
	else:
		MainTool.toast_info("自定义json格式错误")
		pass

